package juc;

public class practice2 {
    public static void main(String[] args) {
        C c = new C();
        Thread thread1 = new Thread(c);
        thread1.setName("T1");
        Thread thread2 = new Thread(c);
        thread2.setName("T2");
        thread1.start();
        thread2.start();
    }
}

class C implements Runnable {
    private int money = 10000;
    @Override
    public void run() {
        while (true){
            // 静态方法对.class加锁，资源属于类
            synchronized (this) {
                if (money < 1000) {
                    System.out.println("余额不足");
                    break;
                }
                money -= 1000;
                System.out.println(Thread.currentThread().getName() + "取1000 " + "余额:" + money);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
